//
//  ViewController.swift
//  ApplePayDemo
//
//  Created by Eric.Wu on 16/2/26.
//  Copyright © 2016年 Eric.Wu. All rights reserved.
//

import UIKit
import PassKit

class ViewController: UIViewController {
    
    @IBOutlet weak var payView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // 1.1、判断设备是否支持apple pay
        if !PKPaymentAuthorizationViewController.canMakePayments() {
            print("该设备不支持apply pay")
        }else if !PKPaymentAuthorizationViewController.canMakePaymentsUsingNetworks([PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay] ) {// 1.2、判断是否添加支付的银行卡
            
            print("还没添加支付银行卡")
            // 添加跳转钱包设置支付卡片的按钮
            let btn = PKPaymentButton(paymentButtonType: PKPaymentButtonType.SetUp, paymentButtonStyle: PKPaymentButtonStyle.Black)
//            btn.backgroundColor = UIColor.redColor()
            btn.addTarget(self, action: "setup", forControlEvents: UIControlEvents.TouchUpInside)
            btn.frame = payView.bounds
            payView.addSubview(btn)
            
        }else {
            
            let btn = PKPaymentButton(paymentButtonType: PKPaymentButtonType.Plain, paymentButtonStyle: PKPaymentButtonStyle.Black)
            btn.addTarget(self, action: "buy", forControlEvents: UIControlEvents.TouchUpInside)
            btn.frame = payView.bounds
            payView.addSubview(btn)
            print("可以使用apple pay")
        }
        
    }
    
    func buy() {
        
        let request = PKPaymentRequest()
        request.merchantIdentifier = ""
        request.countryCode = "CN"
        request.currencyCode = "CNY"
        request.supportedNetworks = [PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay]
        
        let item0 = PKPaymentSummaryItem(label: "点图", amount: NSDecimalNumber(string: "10000"))
        let item1 = PKPaymentSummaryItem(label: "图", amount: NSDecimalNumber(string: "10000000"))
        
        let total = PKPaymentSummaryItem(label: "能图公司", amount: NSDecimalNumber(string: "10010000"))
        
        request.paymentSummaryItems = [item0, item1, total]
        
        request.merchantCapabilities = PKMerchantCapability.Capability3DS
        
        let pavc = PKPaymentAuthorizationViewController(paymentRequest: request)
        pavc.delegate = self
        presentViewController(pavc, animated: true, completion: nil)
        
    }

    func setup() {
        
        // 1.1、判断是否能打开钱包app
        if !PKPassLibrary.isPassLibraryAvailable() {
            
            print("无法打开wallet应用")
            return;
        }
        // 打开钱包设置支付卡
        PKPassLibrary().openPaymentSetup()
        
    }


}

extension ViewController: PKPaymentAuthorizationViewControllerDelegate {
    
    func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: (PKPaymentAuthorizationStatus) -> Void) {
        
        completion(PKPaymentAuthorizationStatus.Success)
        
    }
    
    func paymentAuthorizationViewControllerDidFinish(controller: PKPaymentAuthorizationViewController) {
        
        controller.dismissViewControllerAnimated(true, completion: nil)
        
    }
    
}

